package com.ygy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ygy.entity.Evaluation;
import com.ygy.entity.Users;
import com.ygy.entity.VO.EvaluationVo;
import com.ygy.mapper.UsersMapper;
import com.ygy.service.EvaluationService;
import com.ygy.mapper.EvaluationMapper;
import com.ygy.utils.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author Ye
* @description 针对表【evaluation】的数据库操作Service实现
* @createDate 2024-04-04 09:01:46
*/
@Service
public class EvaluationServiceImpl extends ServiceImpl<EvaluationMapper, Evaluation>
    implements EvaluationService{

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public List<EvaluationVo> geEv(Integer id) {
        QueryWrapper<Evaluation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("receiver_id",id);

        // 拿到所有评价存在evaluationList
        List<Evaluation> evaluationList = list(queryWrapper);
        //遍历所有评价
        //1.创建一个列表来存遍历后更新的评价列表
        ArrayList<EvaluationVo> evaluationVos = new ArrayList<>();
        for (Evaluation evaluation : evaluationList) {//遍历evaluationList
            EvaluationVo evaluationVo = new EvaluationVo();
            BeanUtils.copyProperties(evaluation,evaluationVo);
                //评价表里面没有的遍历后赋值
            if ( evaluation.getBuyerId()!=null) {
                 Users users = usersMapper.selectById(evaluation.getBuyerId());
                evaluationVo.setUsername(users.getUsername());
                evaluationVo.setAvatar(users.getAvatar());
            }else if (evaluation.getSellerId()!=null){
                Users users = usersMapper.selectById(evaluation.getSellerId());
                evaluationVo.setUsername(users.getUsername());
                evaluationVo.setAvatar(users.getAvatar());
            }
            evaluationVos.add(evaluationVo);
        }

        return evaluationVos;
    }

}




